<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="form" :model="formData" :rules="rules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="formData.remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button @click="doLogin" type="primary" class="login_btn"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
      gggg
    <svg-icon class-icon="park"></svg-icon>
    <svg-icon icon-class="park"></svg-icon>
    </div>
    
  </div>
</template>

<script>
const FORMDATA_KEY = "form_key";

export default {
  name: "Login",
  data() {
    return {
      // 表单数据对象，包含账号、密码和记住我选项
      formData: {
        username: "",
        password: "",
        remember: "",
      },
      // 表单验证规则
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  // 组件挂载后执行的生命周期钩子
  mounted() {
    // 从本地存储中获取表单数据
    const cacheFormStr = localStorage.getItem(FORMDATA_KEY);
    if (cacheFormStr) {
      // 解析存储的 JSON 字符串
      const cacheFormData = JSON.parse(cacheFormStr);
      // 更新表单数据
      this.formData.username = cacheFormData.username;
      this.formData.password = cacheFormData.password;
    }
  },
  methods: {
    doLogin() {
      // 验证表单
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            // 解构赋值获取账号和密码
            const { username, password } = this.formData;
            // 调用 Vuex store 中的 login action 进行登录
            await this.$store.dispatch("user/login", { username, password });
            // remeber逻辑
            if (this.formData.remember) {
              // 如果勾选了“记住我”，将表单数据存储到本地存储
              localStorage.setItem(FORMDATA_KEY, JSON.stringify({ username, password }))
            } else {
              // 如果未勾选“记住我”，移除本地存储中的表单数据
              localStorage.removeItem(FORMDATA_KEY)
            }
            this.$message.success("登录成功");
            // 跳转到主页
            this.$router.push("/");
          } catch (error) {
            // 捕获并处理登录过程中可能出现的错误
            console.error(error);
          }
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.login_body {
  display: flex;
}
.bg {
  width: 60vw;
  height: 100vh;
  background: url("~@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}
.box {
  margin: 200px 10% 0;
  flex: 1;
  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }
  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }
    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }
    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}
.login_btn {
  width: 100%;
}
</style>
